<template>
    <div class="layersty" >
        <FormBoListPreviewList ref="boList" :config="config" :html="html" :customVm="getCurVm"></FormBoListPreviewList>
    </div>
</template>

<script>

import FormBoListPreviewList from "@/views/modules/form/core/FormBoListPreviewList";
import FormBoListApi from "@/api/form/core/formBoList";
import PublicApi from "@/api/form/core/public";
import BusEvent from "@/views/modules/form/core/formComponent/BusEvent";

export default {
    name: "list-component",
    components: {FormBoListPreviewList},
    mixins: [BusEvent],
    props: {
        config: {
            type: Object,
            default: () => {
                return {}
            }
        },
        layoutItem: {
            type: Object,
            default: () => {
                return {}
            }
        },
        isTab: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            currentView: {
                template: "<div>" + this.$ti18('rxCommon.Loading','正在加载...') + "</div>"
            },
            html: ""
        }
    },
    created() {
        var self_ = this;
        this.$bus.on('refreshEvent', (args) => {
            self_.$refs.boList.loadData();
        })
    },
    mounted() {
        this.loadList();
    },
    methods: {
        getCurVm() {
            return this;
        },
        getData() {
            return this.$refs.boList.$refs.current.$refs.table.getData();
        },
        getSelectedRow() {
            if(this.$refs.boList.$refs.current.formBoList.dataStyle=='tree'){
                return this.$refs.boList.$refs.current.$refs.table.getSelectedNode();
            }
            return this.$refs.boList.$refs.current.$refs.table.getSelectedRow();
        },
        getSelectedRows() {
            if(this.$refs.boList.$refs.current.formBoList.dataStyle=='tree'){
                return this.$refs.boList.$refs.current.$refs.table.getSelectedNodes();
            }
            return this.$refs.boList.$refs.current.$refs.table.getSelectedRows();
        },
        getParent(name) {
            var vmObj=this;
            while(!vmObj.isFormCustom){
                vmObj=vmObj.$parent;
            }
            var ctlObj=vmObj.$refs[name];
            if(!ctlObj){
                var layAry=vmObj.$refs;
                for(var lay in layAry){
                    for(var i=0;i<layAry[lay].length;i++){
                        var layObj=layAry[lay][i].$refs[name];
                        if(layObj){
                            return layObj;
                        }
                    }
                }
            }
            return ctlObj;
        },
        loadList() {
            if (!this.config.alias) {
                return;
            }
            var parameter = {key: this.config.alias, params: '',pmtAlias:this.config.pmtAlias};
            var params = {};
            //取路由参数
            var params=this.getRouterParams();
            //读取配置的参数
            for (var i = 0; i < this.config.receive.mapping.length; i++) {
                var o = this.config.receive.mapping[i];
                if(o.valueSource == 'param'){
                    if(this.config.receive.type== "url"){
                        if (params[o.valueDef]) {
                            parameter.params += o.name + '=' + params[o.valueDef] + '&';
                        }
                    }else {
                        if (params[o.name]) {
                            parameter.params += o.name + '=' + params[o.name] + '&';
                        }
                    }
                }
            }
            var self = this;
            this.getParams(function (params) {
                for (var key in params) {
                    parameter.params += key + '=' + params[key] + '&';
                }
                parameter.params = parameter.params.substring(0, parameter.params.length - 1);
                FormBoListApi.dialog(parameter).then(res => {
                    if(!res.success){
                        return;
                    }
                    var record = res.data;
                    self.html = record.listHtml;
                });
            });
        },
        handReceive(args) {
            var receive = this.config.receive;

            if (this.config.receive.type == 'event' && receive.publishComponent == args.component) {
                var inputParams = args.params;
                var params = {};
                //读取配置的参数
                for (var i = 0; i < receive.mapping.length; i++) {
                    var o = receive.mapping[i];
                    //处理没有配置值来源的情况。
                    if(!o.valueDef && inputParams[o.name]){
                        params[o.name]=inputParams[o.name]
                    }
                    else if (o.valueSource == 'param' && inputParams[o.valueDef]) {
                        //日期范围
                        if(o.type && o.type=='rangepicker' && o.values && inputParams[o.valueDef]){
                            params[o.values[0]]=inputParams[o.valueDef][0];
                            params[o.values[1]]=inputParams[o.valueDef][1];
                        }else {
                            params[o.name] = inputParams[o.valueDef];
                        }
                    }else {
                        params[o.name]="";
                    }
                }
                this.getParams(function (data,self_) {
                    for (var key in data) {
                        params[key] = data[key];
                    }
                    self_.$refs.boList.loadByParams(params);
                })
            }
        },
        getParams(callback) {
            var self=this;
            PublicApi.getParams(JSON.stringify(this.config.receive.mapping)).then(res => {
                callback(res,self);
            }).catch(err => {
                callback({},self);
            })
        },
        /**
         * 处理页面定制公共事件
         *
         * @param args
         * {
         *     //方法数据
         *     method:"",
         *     //数据
         *     data:{},
         *     //查询参数:配置参数使用
         *     params:[]
         * }
         */
        handPublicReceive(args){
            //如果调用自定义的方法，则进行调用。
            if(args.method){
                this.$refs.boList.executeMethod(args.method,args.data);
                return ;
            }

            var receive = this.config.receive;
            var inputParams = args.params;
            var params = {};
            //读取配置的参数
            for (var i = 0; i < receive.mapping.length; i++) {
                var o = receive.mapping[i];
                //处理没有配置值来源的情况。
                if(inputParams[o.name]){
                    params[o.name]=inputParams[o.name];
                }
                else {
                    params[o.name]="";
                }
            }
            this.getParams(function (data,self_) {
                for (var key in data) {
                    params[key] = data[key];
                }
                if(self_.$refs.boList){
                    self_.$refs.boList.loadByParams(params);
                }
            })
        }
    },
    watch: {
        config: {
            handler: function (val, oldVal) {
                if (val) {
                    this.loadList();
                }
            },
            deep: true
        }
    }
}
</script>

<style scoped>
.layersty {
    height: 100%;
    border: 1px solid #dadde0;
    background: #fff;
    overflow-y: auto;
}


.font_tab_btn {
    background: #fafafa;
    padding: 5px 10px;
    border-bottom: 1px solid #dadde0;
    font-size: 14px;
    position: absolute;
    top: 40px;
    width: calc(100% - 3px);
    z-index: 19;
}
.hr_btn {
    position: relative;
    bottom: -34px;
    z-index: 99;
    margin: 0;
    opacity: .4;
}
</style>